package com.module.lemlin.base

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.os.Build
import android.os.Bundle
import android.view.*
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.ToastUtils
import com.module.lemlin.R
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.EventBus.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

abstract class BaseAbstractFragment : Fragment(), View.OnTouchListener {

    private var isFirstLoad = false

    /**
     * 需要绑定的视图ID
     */
    abstract val layoutResId: Int

    private var rootView: View? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return rootView ?: inflater.inflate(layoutResId, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (!getDefault().isRegistered(this))
            getDefault().register(this)
        view.setOnTouchListener(this)
        doBeforeInit()
        initView()
    }

    override fun onResume() {
        super.onResume()
        if (!isFirstLoad) {
            initData()
            isFirstLoad = true
        } else {
            onRefreshLoad()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        getDefault().unregister(this)
    }

    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        return true
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onEventMainThread(any: Any) {
    }

    /**
     * 设置沉浸式状态栏
     * */
    private fun initStatusBar() {

        //设置状态栏模式
        BarUtils.setStatusBarLightMode(
            requireActivity(), stateBar().isLightMode
        )
        //设置状态栏颜色
        BarUtils.setStatusBarColor(
            requireActivity(), ColorUtils.getColor(stateBar().color)
        )
        //为标题栏添加状态栏的高度
        stateBar().view?.let {
            BarUtils.addMarginTopEqualStatusBarHeight(it)
        }
        //设置底部菜单栏颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            BarUtils.setNavBarColor(
                requireActivity().window,
                ColorUtils.getColor(stateBar().bottomColor)
            )
        }
    }


    fun createViewEmptyNull(hint: String): View {
        return layoutInflater.inflate(R.layout.view_empty_null, null).apply {
            findViewById<TextView>(R.id.tvEmptyNullHint).text = hint
        }
    }

    fun createViewEmptyLoad(): View {
        return layoutInflater.inflate(R.layout.view_empty_load, null)
    }

    fun createViewFooter(hint: String = "没有更多数据"): View =
        TextView(requireContext()).apply {
            layoutParams = FrameLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                ConvertUtils.dp2px(30f)
            )
            gravity = Gravity.CENTER
            textSize = 13f
            text = hint
            setTypeface(null, Typeface.BOLD)
            setTextColor(Color.parseColor("#CCCCCC"))
        }

    /**
     * 在初始化视图之前的操作
     */
    open fun doBeforeInit() = Unit

    /**
     * 初始化UI
     */
    open fun initView() = Unit

    /**
     * 初始化数据
     */
    open fun initData() = Unit


    /**
     * 刷新数据
     */
    open fun onRefreshLoad() = Unit

    /**
     * toast提示
     * */
    fun toast(code: Int? = 0, message: String?) {
        if (code != 401)
            ToastUtils.showShort(message)
    }

    /**
     *
     * */
    open fun stateBar(): StatusBar = StatusBar()

    data class StatusBar(
        var isLightMode: Boolean = true,
        @ColorRes var color: Int = android.R.color.transparent,
        @ColorRes var bottomColor: Int = android.R.color.white,
        var view: View? = null
    )
}